<template>
  <d-modal :title="(type === '0' ? '新增' : '修改') + '用户'" :visible="visible" :width="400" @ok="handleSubmit"
    @cancel="handleCancel">
    <a-form ref="formRef" :model="formState" :rules="rules" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }">
      <a-form-item label="用户名" name="userName">
        <a-input v-model:value="formState.userName" placeholder="请输入用户名！" />
      </a-form-item>
      <a-form-item label="登录名" name="loginName">
        <a-input v-model:value="formState.loginName" placeholder="请输入登录名！" />
      </a-form-item>
      <a-form-item label="密码" name="userPwd">
        <a-input-password v-model:value="formState.userPwd" placeholder="请输入密码！" />
      </a-form-item>
      <a-form-item label="用户状态" name="userStatus">
         <a-radio-group v-model:value="formState.userStatus" button-style="solid">
          <a-radio-button value="0">禁用</a-radio-button>
          <a-radio-button value="1">启用</a-radio-button>
         </a-radio-group>
      </a-form-item>
      <a-form-item label="手机号" name="userMobTel">
        <a-input v-model:value="formState.userMobTel" placeholder="请输入手机号！" />
      </a-form-item>
    </a-form>
  </d-modal>
</template>
<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import { save, update } from '@/api/user/index'

const emits = defineEmits(['ok'])
const visible = ref(false)
const type = ref('0')
const formRef = ref()
const formState = ref({ userStatus: '1' })
const rules = ref({
  userName: [{ required: true, message: '请输入用户名！' }],
  loginName: [{ required: true, message: '请输入登录名！' }],
  userPwd: [{ required: true, message: '请输入密码！' }],
  userStatus: [{ required: true, message: '请选择用户状态！' }]
})

const add = (t) => {
  type.value = t
  formState.value = { userStatus: '1' }
  visible.value = true
}

const edit = (t, record) => {
  type.value = t
  formState.value = record
  visible.value = true
}

const handleCancel = () => {
  visible.value = false
}

const handleSubmit = () => {
  formRef.value.validate().then(() => {
    (type.value === '0' ? save(formState.value) : update(formState.value)).then((res) => {
      if (res.code === 0) {
        emits('ok')
        handleCancel()
      } else {
        message.error(res.message)
      }
    })
  })
}

defineExpose({
  add,
  edit
})
</script>